const Controller = require('egg').Controller;
const path = require('path');
const fs = require('fs');
const sendYzm = require('../utils/emil');
/**
 * @Controller 公共接口
 */
class BaseController extends Controller {
  Success(data = {}) {
    const { ctx } = this;
    ctx.body = {
      code: 200,
      ...data,
    };
  }
  Error(data = {}) {
    const { ctx } = this;
    ctx.body = {
      code: 500,
      ...data,
    };
  }
  // 上传图片
  async uploadImage() {
    const { ctx, service } = this;
    //获取文件
    const file = ctx.request.files[0];
    //读取文件 Buffer格式
    let newFile = fs.readFileSync(file.filepath);
    //设置上传路径
    const dirPath = __dirname.replace(/controller/g, '');
    //设置时间戳
    let time = Date.now();
    //base路径
    let basePath =
      '/public/avatar/upload/' + time + path.basename(file.filename);
    //设置文件路径
    const filePath = path.join(dirPath, basePath);
    //写入文件
    fs.writeFile(filePath, newFile, 'binary', (err) => {
      console.log(err);
    });
    ctx.cleanupRequestFiles();
    //处理完成的文件链接
    let url = ctx.request.origin + basePath;

    // console.log(url);
    //返回前端
    this.Success({ url, msg: '上传成功' });
  }
  // 上传文件
  async uploadFile() {
    const { ctx, service } = this;
    //获取文件
    const file = ctx.request.files[0];
    //读取文件 Buffer格式
    let newFile = fs.readFileSync(file.filepath);
    //设置上传路径
    const dirPath = __dirname.replace(/controller/g, '');
    //设置时间戳
    let time = Date.now();
    //base路径
    let basePath = '/public/file/upload/' + time + path.basename(file.filename);
    //设置文件路径
    const filePath = path.join(dirPath, basePath);
    //写入文件
    fs.writeFile(filePath, newFile, 'binary', (err) => {
      console.log(err);
    });
    ctx.cleanupRequestFiles();
    //处理完成的文件链接
    let url = 'https://lengnuanit.top' + basePath;
    // let url = ctx.request.origin + basePath
    // console.log(url);
    //返回前端
    ctx.body = {
      code: 200,
      msg: '上传文件成功',
      data: {
        code: 200,
        url: url,
      },
    };
  }
  /**
   * @Summary 发送邮件验证码
   * @router post /api/base/email
   * @Request body emailBaseRequest
   */
  async email() {
    const { ctx, app } = this;
    const { email } = ctx.request.body;
    let YZM_CODE = '';
    for (let i = 0; i < 6; i++) {
      YZM_CODE += Math.floor(Math.random() * 10);
    }
    await app.redis.set('YZM_CODE', YZM_CODE, 'Ex', '60');

    sendYzm.send(email, YZM_CODE);
    this.Success({
      code: 200,
      msg: '验证码已发送您的邮箱，清注意查收',
    });
  }
  /**
   * @Summary 七牛云文件上传
   * @router post /api/base/upload
   * @Request body uploadBaseRequest
   */
  async upload() {
    const { ctx } = this;
    const data = await ctx.service.base.upload();
    if (data) {
      this.Success({ data, message: '上传成功' });
    } else {
      this.Error({ message: '上传失败' });
    }
  }
  /**
   * @Summary 获取七牛云上传Token
   * @router post /api/base/token
   * @Request body tokenBaseRequest
   */
  async token() {
    const { ctx } = this;
    const data = await ctx.service.base.token();
    if (data) {
      this.Success({ data, message: '请求成功' });
    } else {
      this.Error({ message: '请求失败' });
    }
  }

  /**
   * @Router post /api/base/area/tree
   * @Summary 获取所有行政区域
   */
  async tree() {
    const { ctx, app, service } = this;
    const body = ctx.request.body;
    const data = await service.base.tree();
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }

  /**
   * @Router post /api/base/job/tree
   * @Summary 获取所有职务
   */
  async job() {
    const { ctx, app, service } = this;
    const body = ctx.request.body;
    const data = await service.base.job();
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }

  /**
   * @Router post /api/base/province
   * @Summary 获取所有省份
   */
  async province() {
    const { ctx, app, service } = this;
    const body = ctx.request.body;
    const data = await service.base.province();
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }

  /**
   * @Router post /api/base/city
   * @Summary 获取某个省份下面的所有城市
   */
  async city() {
    const { ctx, app, service } = this;
    const body = ctx.request.body;
    const data = await service.base.city(body);
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }
}

module.exports = BaseController;
